package com.hzgj.bcl.util.lang;

import java.util.concurrent.*;

/**
 * Created by Administrator on 2016/10/13.
 */
public class ThreadPoolKit {

    public static ScheduledExecutorService getScheduledExecutorService(String name) {
        return Executors.newScheduledThreadPool(1, r -> {
            Thread t = new Thread(r);
            t.setName(name + System.currentTimeMillis());
            t.setDaemon(true);
            return t;
        });
    }

    public static ThreadPoolExecutor getThreadPool(int corePoolSize,
                                                   int maximumPoolSize,
                                                   long keepAliveTime,
                                                   TimeUnit unit,
                                                   int queueSize) {
        return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime,
                unit, new ArrayBlockingQueue<>(queueSize),
                r -> {
                    Thread t = new Thread(r);
                    t.setName("tp-" + System.currentTimeMillis());
                    t.setDaemon(true);
                    return t;
                },
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
}
